﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Grid;
using NNS.Enumeration;
using NNS.Framework;
using NNS.UI;
using QLTB.NSX.Entity;

namespace QLTB.NSX.UI
{
    [System.ComponentModel.ToolboxItem(false)]
    public partial class UcNsx : MyUIBase
    {
        private readonly DataView dv;

        public UcNsx()
        {
            InitializeComponent();
            Nap();
            //TBL_quyenBL.IgnoreError = true;
            //if (!TBL_quyenBL.isTaomoi(EnumComponent.iProvider))
            //{
            //    gbList.OptionsView.NewItemRowPosition = NewItemRowPosition.None;
            //}
            //TBL_quyenBL.IgnoreError = false;

            dsNSX.TBL_nhasx.TBL_nhasxRowChanging += TBL_nhasx_TBL_nhasxRowChanging;
            gcList.DataSource = dv = dsNSX.TBL_nhasx.DefaultView;
        }

        public bool isFilter
        {
            get { return gbList.OptionsView.ShowAutoFilterRow; }

            set { gbList.OptionsView.ShowAutoFilterRow = value; }
        }

        private void TBL_nhasx_TBL_nhasxRowChanging(object sender, dsNSX.TBL_nhasxRowChangeEvent e)
        {
            //if (Mode == EditMode.Load)
            //{

            //    return;
            //}
            //if (Mode == EditMode.Update) return;
            //if (e.Action == DataRowAction.Change)
            //{
            //    if (!TBL_quyenBL.isThaydoi(EnumComponent.iProvider))
            //    {
            //        Nap();
            //        NotSave = false;
            //        return;
            //    }
            //}
        }


        public void Nap()
        {
            Mode = EditMode.Load;
            dsNSX.TBL_nhasx.Rows.Clear();
            SqlHelper.Helper.Fill(CommandType.Text, "SELECT id, nhasx FROM TBL_nhasx ORDER BY id", dsNSX.TBL_nhasx);
            Mode = EditMode.None;
        }

        public override void Save()
        {
            Mode = EditMode.Update;

            try
            {
                tBL_nhasxTableAdapter.Connection.ConnectionString = DbHelper.ConnectionString;
                tBL_nhasxTableAdapter.Adapter.RowUpdated += Adapter_RowUpdated;
                tBL_nhasxTableAdapter.Update(dsNSX.TBL_nhasx);
                NotSave = false;
                dsNSX.AcceptChanges();
            }
            catch (Exception ex)
            {
                ShowWarning(ex.Message, "Quản lý nhà sản xuất");
            }
            OnSaveComplete(null);
            Mode = EditMode.None;
        }

        private void Adapter_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
        {
            OnSaveChanging(new SaveChangingEventArgs(e.RowCount, dsNSX.TBL_nhasx.Count));
            //throw new NotImplementedException();
        }

        private void gbList_ValidateRow(object sender, ValidateRowEventArgs e)
        {
            ValidateData();
        }

        private void ValidateData()
        {
            //for (int i = 0; i < gbList.RowCount; i++)
            //{

            //}
        }

        private void gbList_CellValueChanged(object sender, CellValueChangedEventArgs e)
        {
            NotSave = true;
            OnSaveChanged(new SaveChangedEventArgs {NotSave = true});
        }

        private void ucNHACUNGCAP_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                Deleted();
            }
        }

        public void Deleted()
        {
            //if (!TBL_quyenBL.isThaydoi(EnumComponent.iProvider)) return;

            if (ShowQuestion("Bạn thật sự muốn xóa không?", "Quản lý nhà sản xuất") == DialogResult.No) return;
            if (gbList.SelectedRowsCount > 0)
            {
                gbList.DeleteSelectedRows();
            }
            else
            {
                gbList.DeleteRow(gbList.FocusedRowHandle);
            }
        }

        public void SelectAll()
        {
            gbList.SelectAll();
        }

        public void Copy()
        {
            gbList.CopyToClipboard();
        }

        public void ClearContext()
        {
            if (ShowQuestion("Bạn thật sự muốn xóa không?", "Quản lý nhà sản xuất") == DialogResult.No) return;
            gbList.SelectAll();
            gbList.DeleteSelectedRows();
        }

        public void Cancel()
        {
            dsNSX.RejectChanges();
            NotSave = false;
        }

        private void gbList_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            int rowIndex = e.RowHandle;
            if (rowIndex >= 0)
            {
                rowIndex++;
                e.Info.DisplayText = rowIndex.ToString();
            }
        }

        private void gbList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Delete)
            {
                Deleted();
            }
        }

        private void txtTimnhanh_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Filter();
            }
        }

        private void Filter()
        {
            dv.RowFilter = "nhasx like '%" + txtTimnhanh.Text + "%'";
        }

        private void txtTimnhanh_EditValueChanged(object sender, EventArgs e)
        {
            if (!chxAuto.Checked) return;
            Filter();
        }

        private void txtTimnhanh_Properties_ButtonClick(object sender, ButtonPressedEventArgs e)
        {
            txtTimnhanh.Text = "";
            dv.RowFilter = "";
        }
    }
}